package repositorio;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Aula;
import model.Turma;

public class AulaRepositorio {

	
	public boolean incluir(Aula entidade) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("INSERT INTO AULA (CD_AULA, " +
														"CD_TURMA, " +
														"DATA) " +
												   "VALUES (?, ?, ?)");
			ps.setLong(1, entidade.getCdAula());
			ps.setString(2, entidade.getTurma().getCdTurma());
			ps.setDate(3, new java.sql.Date(entidade.getData().getTime()));
			
			return (ps.executeUpdate() > 0);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
	}

	
	public List<Aula> buscarAulasPorTurma(String cdTurma) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Aula> aulas =null;
		Aula aula = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT * " +
									  "FROM AULA " +
									  "WHERE CD_TURMA= ? " +
									  "ORDER BY DATA");
			ps.setString(1, cdTurma);
			rs = ps.executeQuery();
			
			aulas = new ArrayList<Aula>();
			
			while(rs.next()){
				aula = new Aula();
				aula.setCdAula(rs.getLong("CD_AULA"));
				
				Turma t = new Turma();
				t.setCdTurma(rs.getString("CD_TURMA"));
				
				aula.setTurma(t);
				aula.setData(new Date(rs.getDate("DATA").getTime()));
				aula.setConteudo(rs.getString("CONTEUDO"));
				
				aulas.add(aula);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
		return aulas;
	}

	
	public boolean remover(Aula entidade) {
		// TODO Auto-generated method stub
		return false;
	}

	
	public List<Aula> buscarTodos() {
		// TODO Auto-generated method stub
		return null;
	}

	
	public Aula buscarPorPk(Aula entidade) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public boolean criarChamada(int matriculaAluno, long cdAula){
		
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("INSERT INTO CHAMADA (MATRICULA_ALUNO, CD_AULA) " +
															"VALUES (?, ?)");
			ps.setInt(1, matriculaAluno);
			ps.setLong(2, cdAula);
			
			return (ps.executeUpdate() > 0);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
		
		
		
		
	}
	
	public Map<String, String> buscarChamdasAluno(int matriculaAluno, String cdTurma){
		ResultSet rs = null;
		Connection con = null;
		PreparedStatement ps = null;
		
		Map<String, String> chamadas = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT C.FALTAS, " +
											 "A.DATA " +
									  "FROM CHAMADA C " +
									  "INNER JOIN AULA A ON A.CD_AULA = C.CD_AULA " +
									  "WHERE C.MATRICULA_ALUNO = ? " +
									    "AND A.CD_TURMA = ? " +
									  "ORDER BY A.DATA");
			ps.setInt(1, matriculaAluno);
			ps.setString(2, cdTurma);
			rs = ps.executeQuery();
			
			chamadas = new HashMap<String, String>();
			
			while(rs.next()){
				
				chamadas.put(new SimpleDateFormat("dd/MM").format(new Date(rs.getDate("DATA").getTime())) ,
										rs.getString("FALTAS"));
				
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
		
		return chamadas;
		
		
	}
	
	
	
	

}
